package com.web.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/CheckCode")
public class CheckCode extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        //获取验证码并储存
        String code = req.getParameter("code");



        String randomImg =(String)req.getSession().getAttribute("randomImg");

        //移除sission存储的验证码
        req.getSession().removeAttribute("randomImg");


        if(code == null || code.trim().equals("")){
            resp.getWriter().write("验证码不能为空");
            System.out.println("空验证码");
        }else if (code.equalsIgnoreCase(randomImg)||code.trim().equalsIgnoreCase(randomImg)) {
            resp.getWriter().write("验证码正确");
            System.out.println("正确");
        }else {
            resp.getWriter().write("验证码错误");
            System.out.println("错误");
        }
    }
}
